package com.example.smart_city_0829.activity.lawyer_consultation.fragment;

import android.content.Context;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.smart_city_0829.R;
import com.example.smart_city_0829.base.BaseFragment;
import com.example.smart_city_0829.base.CommonAdapter;
import com.example.smart_city_0829.entity.lawyer_onsultation.Comment;
import com.example.smart_city_0829.entity.lawyer_onsultation.LawyerList;
import com.example.smart_city_0829.retrofit.NetCallBack;
import com.example.smart_city_0829.utils.Constant;
import com.google.gson.reflect.TypeToken;

import java.util.List;

import retrofit2.Call;
import retrofit2.Response;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link CommentListFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class CommentListFragment extends BaseFragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public CommentListFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment CommentListFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static CommentListFragment newInstance(String param1, String param2) {
        CommentListFragment fragment = new CommentListFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    private ListView lv;
    private TextView noCommentTv;




    @Override
    protected int getLayoutId() {
        return R.layout.fragment_comment_list;
    }

    @Override
    protected void initView() {
        lv = (ListView) rootView.findViewById(R.id.lv);
        noCommentTv = (TextView) rootView.findViewById(R.id.no_comment_tv);

    }

    @Override
    protected void initData() {
        final LawyerList.RowsBean lawyer = gson.fromJson(mParam1, new TypeToken<LawyerList.RowsBean>() {}.getType());

        api.getLawyerComment(lawyer.getId()).enqueue(new NetCallBack<Comment>() {
            @Override
            public void onResponse(Call<Comment> call, Response<Comment> response) {
                if (response.body() == null) return;
                if (response.body().getRows().size() == 0){
                    noCommentTv.setVisibility(View.VISIBLE);
                }else {
                    lv.setAdapter(new LvAdapter(response.body().getRows(),getContext()));
                }
            }
        });
    }

    @Override
    protected void initEvent() {

    }

    static class LvAdapter extends CommonAdapter<Comment.RowsBean,CommonAdapter.CommentListViewHolder>{

        public LvAdapter(List<Comment.RowsBean> data, Context context) {
            super(data, context);
        }

        @Override
        protected void setData(Comment.RowsBean data, int position, CommentListViewHolder viewHolder, View convertView, Context context) {
            viewHolder.im.setVisibility(View.VISIBLE);

            Glide.with(context)
                    .load(Constant.BASE_URL + data.getFromUserAvatar())
                    .into(viewHolder.im);
            viewHolder.name.setText(String.valueOf(data.getFromUserName()));
            viewHolder.content.setText(String.valueOf(data.getEvaluateContent()));
            viewHolder.tag1.setText(String.valueOf(data.getCreateTime()));

            viewHolder.cb.setText(String.valueOf(data.getLikeCount()));

            viewHolder.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked){
                        data.setLikeCount(data.getLikeCount() + 1);
                    }else {
                        data.setLikeCount(data.getLikeCount() - 1);
                    }
                    notifyDataSetChanged();
                }
            });
        }

        @Override
        protected CommentListViewHolder getViewHolder() {
            return new CommentListViewHolder();
        }

        @Override
        protected void initView(CommentListViewHolder viewHolder, View convertView) {
            viewHolder.im = (ImageView) convertView.findViewById(R.id.im);
            viewHolder.name = (TextView) convertView.findViewById(R.id.name);
            viewHolder.content = (TextView) convertView.findViewById(R.id.content);
            viewHolder.tag1 = (TextView) convertView.findViewById(R.id.tag1);
            viewHolder.cb = (CheckBox) convertView.findViewById(R.id.cb);
        }

        @Override
        protected int getItemLayoutResourceId() {
            return R.layout.comment_list_item;
        }
    }

}